
#include "InvoiceModelDelegate.h"
#include "InvoicesModel.h"

#include <QDoubleSpinBox>

using namespace ecocity;

InvoiceModelDelegate::InvoiceModelDelegate( QObject *parent )
        : QItemDelegate( parent )
{
}

//---------------------------------------------------------------

void
InvoiceModelDelegate::paint( QPainter *painter,
                             const QStyleOptionViewItem &option,
                             const QModelIndex &index) const
{
    switch ( index.column( ) )
    {
    case InvoicesModel::INVOICE_TOTAL_PRICE:
    case InvoicesModel::INVOICE_TOTAL_TAXES:
    case InvoicesModel::INVOICE_TOTAL_PROFIT:
    case InvoicesModel::INVOICE_TOTAL_WITHOUT_TAX:
    {
        float amount = index.model()->data( index, Qt::DisplayRole ).toFloat( );
        QString text = QString::number( amount, 'f', 2 );
        text.append( QString::fromUtf8( " €" ) );

        if ( index.column( ) == InvoicesModel::INVOICE_TOTAL_PROFIT
             && amount == 0 )
        {
            // Should be zero if customer.type == FRIENDS
            text.prepend( "(*) " );
        }

        QStyleOptionViewItem my_option = option;

        my_option.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
        drawDisplay( painter, my_option, my_option.rect, text );
        drawFocus( painter, my_option, my_option.rect );
        break;
    }
    default:
        QItemDelegate::paint( painter, option, index );
        break;
    }
}

//---------------------------------------------------------------

QWidget*
InvoiceModelDelegate::createEditor( QWidget *parent,
                                    const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
{
    switch ( index.column( ) )
    {
    case InvoicesModel::INVOICE_TOTAL_PRICE:
    case InvoicesModel::INVOICE_TOTAL_TAXES:
    case InvoicesModel::INVOICE_TOTAL_PROFIT:
    case InvoicesModel::INVOICE_TOTAL_WITHOUT_TAX:
    {
        QDoubleSpinBox* spin_box = new QDoubleSpinBox( parent );
        spin_box->setDecimals( 2 );
        connect( spin_box, SIGNAL( editingFinished( ) ),
                 this, SLOT( commitAndCloseEditor( ) ) );

        return spin_box;
    }
    default:
        return QItemDelegate::createEditor( parent, option, index );
    }
}

//---------------------------------------------------------------

void
InvoiceModelDelegate::commitAndCloseEditor( )
{
    QDoubleSpinBox *editor = qobject_cast<QDoubleSpinBox *>(sender( ));
    emit commitData( editor );
    emit closeEditor( editor );
}

//---------------------------------------------------------------

void
InvoiceModelDelegate::setEditorData( QWidget *editor,
                                     const QModelIndex &index ) const
{
    switch ( index.column( ) )
    {
    case InvoicesModel::INVOICE_TOTAL_PRICE:
    case InvoicesModel::INVOICE_TOTAL_TAXES:
    case InvoicesModel::INVOICE_TOTAL_PROFIT:
    case InvoicesModel::INVOICE_TOTAL_WITHOUT_TAX:
    {
        float price = index.model()->data( index, Qt::DisplayRole).toFloat( );
        QDoubleSpinBox *price_edit = qobject_cast< QDoubleSpinBox *>(editor);
        price_edit->setValue( price );
        break;
    }
    default:
        QItemDelegate::setEditorData( editor, index );
        break;
    }
}

//---------------------------------------------------------------

void
InvoiceModelDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
                                    const QModelIndex &index) const
{
    switch ( index.column( ) )
    {
    case InvoicesModel::INVOICE_TOTAL_PRICE:
    case InvoicesModel::INVOICE_TOTAL_TAXES:
    case InvoicesModel::INVOICE_TOTAL_PROFIT:
    case InvoicesModel::INVOICE_TOTAL_WITHOUT_TAX:
    {
        QDoubleSpinBox *price_edit = qobject_cast< QDoubleSpinBox *>(editor);
        float price = price_edit->value( );
        model->setData( index, price );
        break;
    }
    default:
        QItemDelegate::setModelData( editor, model, index );
        break;
    }
}

